package com.mhxy.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.mhxy.constant.PicConstants;
import com.mhxy.entity.GameLoginInfo;
import com.mhxy.handler.ConfigHandler;
import com.mhxy.handler.TaskProgressHandler;
import com.mhxy.service.AbstractTask;
import com.mhxy.util.FuzzyPicUtil;
import com.mhxy.util.HwndUtil;
import com.mhxy.util.ThreadUtil;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LoginGameTaskImpl extends AbstractTask {

    private WinDef.HWND loginHwnd;

    private String hwndName;

    private GameLoginInfo gameLoginInfo;

    public LoginGameTaskImpl(WinDef.HWND hwnd_0, String hwndName) {
        super(hwnd_0);
        this.hwndName = hwndName;
    }

    @Override
    public Boolean before() throws Exception {
        ThreadUtil.sleep3();
        loginHwnd = HwndUtil.getLoginHwnd(HwndUtil.hwndsMap.get(hwndName).getProcessId());
        if (loginHwnd == null) {
            log.info("【{}】登录句柄不存在", getTaskName());
            return null;
        }
        gameLoginInfo = ConfigHandler.getCurrentGameLoginInfo();
        log.info("【{}】获取当配置账号信息: {}", getTaskName(), gameLoginInfo);
        if (gameLoginInfo == null) {
            log.error("【{}】当前配置账号信息不存在", getTaskName());
            return null;
        }
        // 这里添加一个随机休眠数，防止多个窗口同步登录造成网络异常
        long time = RandomUtil.randomLong(2000, 20000);
        log.info("【{}】随机休眠时间={}ms", getTaskName(), time);
        ThreadUtil.sleep(time);
        return true;
    }

    @Override
    public Boolean execute() throws Exception {
        String loginAccountPic = gameLoginInfo.getLoginAccountPic();
        log.info("loginAccountPic={}", loginAccountPic);
        // 选择登录账号
        if (!FuzzyPicUtil.findPic(loginHwnd, loginAccountPic, 3).isFinded()) {
            if (!FuzzyPicUtil.findAndClickRandom(loginHwnd, PicConstants.开始游戏_打开账号列表)) {
                return false;
            }
            ThreadUtil.sleep3();
            if (!FuzzyPicUtil.findAndClickRandom(loginHwnd, loginAccountPic)) {
                log.error("【{}】选择登录账号不存在", getTaskName());
                TaskProgressHandler.updateProgress(getTaskName(), "账号不存在");
                return false;
            }
        }

        if (!FuzzyPicUtil.findAndClickRandom(loginHwnd, PicConstants.开始游戏_进入游戏)) {
            return false;
        }
        ThreadUtil.sleep(5000);
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.开始游戏_打开服务列表)) {
            return false;
        }
        ThreadUtil.sleep3();
        // 选择登录服务器
        String loginServerPic = gameLoginInfo.getLoginServerPic();
        log.info("loginServerPic={}", loginServerPic);
        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, loginServerPic, 5, 0.8)) {
            log.error("【{}】选择登录服务器不存在", getTaskName());
            TaskProgressHandler.updateProgress(getTaskName(), "服务不存在");
            return false;
        }
        ThreadUtil.sleep2();
        // 选择登录角色
        String point = StrUtil.subAfter(hwndName, "_", true);
        String loginUserPic = gameLoginInfo.getLoginUserPic(point);
        log.info("loginUserPic={}", loginUserPic);
        if (loginUserPic == null) {
            log.error("【{}】选择登录角色不不存在", getTaskName());
            TaskProgressHandler.updateProgress(getTaskName(), "角色不存在");
            return false;
        }

        // 这里需要按照顺序错开单个登录，防止网络卡顿
        long sleep = RandomUtil.randomLong(3000, 15000);
        log.info("【{}】错开进入游戏sleep={}", getTaskName(), sleep);
        ThreadUtil.sleep();

        if (!FuzzyPicUtil.findAndClickRandom(hwnd_0, loginUserPic, 5, 0.8)) {
            TaskProgressHandler.updateProgress(getTaskName(), "角色不存在");
            return false;
        }

        return true;
    }

    @Override
    public Boolean after() throws Exception {
        ThreadUtil.sleep(10000);
        int times = 10;
        while (!FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_包裹, 1).isFinded() && times > 0) {
            clickEsc1();
            ThreadUtil.sleep1();
            times --;
        }
        return true;
    }

    @Override
    public String getTaskName() throws Exception {
        return "登录游戏";
    }

}
